const redis = require('redis');
const cron = require('node-cron');

const cacheListKey = 'plume_log_list'

const expression = '*/5 * * * * *'

const iotTestClient = redis.createClient({
    url: 'redis://114.217.55.239:6379',
    password: 'ICONMAN20220106R',
    database: 14
}).on('connect', () => {
    console.log('iot_test Connected to Redis');
    iotTestClient.then(client => {
        cron.schedule(expression, () => {
            client.lLen(cacheListKey).then(length => {
                console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>iot_test>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', length);
                for (let i = 0; i < length; i++) {
                    client.lPop(cacheListKey).then(item => {
                    })
                }
            })
        });
    })
}).on('error', (err) => {
    console.error('iot_test Failed to connect to Redis', err);
}).connect();

const iotProdClient = redis.createClient({
    url: 'redis://36.137.54.131:6379',
    password: 'ICONMAN2020406R',
    database: 14
}).on('connect', () => {
    console.log('iot_prod Connected to Redis');
    iotProdClient.then(client => {
        cron.schedule(expression, () => {
            client.lLen(cacheListKey).then(length => {
                console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>iot_prod>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', length);
                for (let i = 0; i < length; i++) {
                    client.lPop(cacheListKey).then(item => {
                    })
                }
            })
        });
    })
}).on('error', (err) => {
    console.error('iot_prod Failed to connect to Redis', err);
}).connect();

const stadiumTestClient = redis.createClient({
    url: 'redis://114.217.55.239:6379',
    password: 'ICONMAN20220106R',
    database: 15
}).on('connect', () => {
    console.log('stadium_test Connected to Redis');
    stadiumTestClient.then(client => {
        cron.schedule(expression, () => {
            client.lLen(cacheListKey).then(length => {
                console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>stadium_test>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', length);
                for (let i = 0; i < length; i++) {
                    client.lPop(cacheListKey).then(item => {
                    })
                }
            })
        });
    })
}).on('error', (err) => {
    console.error('stadium_prod Failed to connect to Redis', err);
}).connect();

const stadiumProdClient = redis.createClient({
    url: 'redis://36.137.54.131:6379',
    password: 'ICONMAN2020406R',
    database: 13
}).on('connect', () => {
    console.log('stadium_prod Connected to Redis');
    stadiumProdClient.then(client => {
        cron.schedule(expression, () => {
            client.lLen(cacheListKey).then(length => {
                console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>stadium_prod>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', length);
                for (let i = 0; i < length; i++) {
                    client.lPop(cacheListKey).then(item => {
                    })
                }
            })
        });
    })
}).on('error', (err) => {
    console.error('stadium_prod Failed to connect to Redis', err);
}).connect();